package com.xiaodou.mjf.web.console.audit.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaodou.mjf.application.core.manager.FinancingManager;
import com.xiaodou.mjf.application.core.service.FinancingAttributeService;
import com.xiaodou.mjf.application.core.service.FinancingService;
import com.xiaodou.mjf.application.core.service.FinancingStatisticsService;
import com.xiaodou.mjf.entity.core.Financing;
import com.xiaodou.mjf.entity.core.Loan;
import com.xiaodou.mjf.infrastructure.common.controller.ApplicationController;

/**
 * 小豆审核Controller
 * 
 * @author BILLY
 *
 */
@Controller
@RequestMapping("audit/xiaodou")
public class XiaoDouAuditController extends ApplicationController {

	@Resource
	FinancingService financingService;
	@Resource
	FinancingStatisticsService financingStatisticsService;
	@Resource
	FinancingAttributeService financingAttributeService;
	@Resource
	FinancingManager financingManager;

	@RequestMapping("")
	public String apply(Loan loan) {

		return "audit/xiaodou/list";
	}

	@RequestMapping(value = { "data.json" })
	@ResponseBody
	public Map<String, Object> data(Financing financing) {
		try {
			financing.setType(Financing.TYPE_XIAODOU);
			Page<Financing> page = financingService.pageAll(financing);
			return page(page);
		} catch (Exception ex) {
			logger.error(ex.toString());
			throw ex;
		}
	}

	@RequestMapping(value = { "commit", "commit/{id}" })
	@ResponseBody
	public boolean audit(@PathVariable String id, Integer audtiStatus,
			String content, Model model) {
		boolean flag = false;
		try {
			flag = financingManager.xiaodouAuditCommit(id, audtiStatus,
					content);
		} catch (Exception ex) {
			flag = false;
			logger.error(ex.toString());
		}

		return flag;
	}
	
	@RequestMapping(value = { "check", "check/{term}" })
	@ResponseBody
	public boolean checkDuplicate(@PathVariable Integer term, Model model) {
		boolean flag = false;
		try {
			Financing financing = financingService.getFinancingByTermAndStatus(term, Financing.INVEST_STATUS_SALE, Financing.TYPE_XIAODOU);
			if(financing!=null) {
				flag = true;
			}
		} catch (Exception ex) {
			flag = false;
			logger.error(ex.toString());
		}
		
		return flag;
	}

}
